{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "\n",
       "require(['notebook/js/codecell'], function(codecell) {\n",
       "  codecell.CodeCell.options_default.highlight_modes[\n",
       "      'magic_text/x-csrc'] = {'reg':[/^%%microblaze/]};\n",
       "  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n",
       "      Jupyter.notebook.get_cells().map(function(cell){\n",
       "          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n",
       "  });\n",
       "});\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# import sys\n",
    "import math\n",
    "import numpy as np\n",
    "import os\n",
    "import time\n",
    "# from PIL import Image\n",
    "from matplotlib import pyplot\n",
    "import cv2\n",
    "# from datetime import datetime\n",
    "from pynq import Xlnk\n",
    "from pynq import Overlay\n",
    "from preprocessing import Agent\n",
    "from preprocessing import BATCH_SIZE\n",
    "from preprocessing import get_image_path\n",
    "from libraries import hwac\n",
    "\n",
    "team = 'hwac_object_tracker'\n",
    "agent = Agent(team)\n",
    "\n",
    "WEIGHT_FILE = '/home/xilinx/jupyter_notebooks/dac_2018/overlay/hwac_object_tracker/weights95tuned.npy'\n",
    "PADDING_FILE = '/home/xilinx/jupyter_notebooks/dac_2018/overlay/hwac_object_tracker/l4padding95.npy'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'tiny_yolo_0': {'phys_addr': 1073741824, 'addr_range': 4096, 'type': 'hwac_tracker:user:tiny_yolo:1.0', 'state': None, 'interrupts': {}, 'gpio': {}, 'fullpath': 'tiny_yolo_0', 'driver': <class 'pynq.overlay.DefaultIP'>}}\n"
     ]
    }
   ],
   "source": [
    "OVERLAY_PATH = '/home/xilinx/jupyter_notebooks/dac_2018/overlay/hwac_object_tracker/hwac_object_tracker.bit'\n",
    "overlay = Overlay(OVERLAY_PATH)\n",
    "print(overlay.ip_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FPGA configured for TinyYOLO v2 | 95 classes\n",
      "Image count: 1000, Batch count: 2\n",
      "1 batchs done\n",
      "2 batchs done\n"
     ]
    }
   ],
   "source": [
    "total_time = 0\n",
    "total_num_img = len(agent.img_list)\n",
    "agent.reset_batch_count()\n",
    "\n",
    "xlnk = Xlnk()\n",
    "xlnk.xlnk_reset()\n",
    "fpga = hwac.FPGA(xlnk)\n",
    "if (fpga.configure(WEIGHT_FILE, PADDING_FILE)):\n",
    "    batch_count = math.ceil(total_num_img/BATCH_SIZE)\n",
    "    print(\"Image count: %d, Batch count: %d\" % (total_num_img, batch_count))\n",
    "    \n",
    "    for i in range(batch_count):\n",
    "        batch = agent.send(0, agent.img_batch)\n",
    "        img_cnt = len(batch)\n",
    "\n",
    "        img_name = get_image_path(batch[0])\n",
    "        batch_start_time = time.time()\n",
    "        fpga.readImage(img_name)\n",
    "\n",
    "        fpga.start()\n",
    "\n",
    "        for j in range(img_cnt) :             \n",
    "            fpga.waitFistLayer()\n",
    "#             print(\"Batch: %d, Image: %d\" % (i, j))    #uncomment to view the progress. comment when measuring time\n",
    "\n",
    "            if(j < img_cnt-1) : \n",
    "                img_name = get_image_path(batch[j + 1])\n",
    "                img = fpga.readImage(img_name)\n",
    "                fpga.stop()\n",
    "                d0, d1, d2 = fpga.WaitBoundingBox()\n",
    "                fpga.start()\n",
    "            else :\n",
    "                fpga.stop()\n",
    "                d0, d1, d2 = fpga.WaitBoundingBox()\n",
    "\n",
    "            fpga.PostProcessBB(d0, d1, d2)\n",
    "        \n",
    "        batch_end_time = time.time()\n",
    "        total_time += (batch_end_time - batch_start_time)\n",
    "        print(\"%d batchs done\" % (i+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total images: 1000, Frames per second: 4.910\n",
      "Coordinate results written successfully.\n",
      "XML results written successfully.\n"
     ]
    }
   ],
   "source": [
    "print(\"Total images: %d, Frames per second: %0.3f\" % (total_num_img, total_num_img/total_time))\n",
    "agent.write(total_time, total_num_img, team)\n",
    "\n",
    "with open(agent.coord_team + '/{}.txt'.format(team), 'w+') as fcoord:\n",
    "    for result_vec in fpga.bb_list :\n",
    "        for element in result_vec:\n",
    "            fcoord.write(str(element))\n",
    "            fcoord.write('\\t')\n",
    "        fcoord.write('\\n')\n",
    "print(\"Coordinate results written successfully.\")\n",
    "\n",
    "agent.save_results_xml(fpga.bb_list)\n",
    "print(\"XML results written successfully.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "xlnk.xlnk_reset()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
